package com.warrior.gatherclient.util;

import com.sun.jna.Platform;
import com.warrior.gatherclient.grpc.Messages;
import com.warrior.gathercommon.constants.CommonStatus;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.text.DecimalFormat;

/**
 * @ClassName CommonUtil.java
 * @Author xujian
 * @Description
 * @CreateTime 2020年12月16日 13:48:00
 */
@Slf4j
public class CommonUtil {
    //换算单位
    private static final double FORMAT = 1024.0;

    public static String getHost() {
        try {
            InetAddress ip4 = Inet4Address.getLocalHost();
            String hostAddress = ip4.getHostAddress();
            return hostAddress;
        } catch (Exception e) {
            log.error("============ 获取ip异常 msg:{} ============", e.getMessage());
        }
        return null;
    }

    public static Messages.Builder initMessage(String targetTypeName) {
        Messages.Builder builder = Messages.newBuilder().setType(CommonStatus.CONSTANT_GRPC_SERVICE_SEND_MESSAGE);
        builder.setMetricType(targetTypeName);
        builder.setIp(getHost());
        return builder;
    }

    public static String executeScript(String[] scriptStr, String[] linuxScript) {
        Process process = null;
        try {
            if (Platform.isWindows()) {
                process = Runtime.getRuntime().exec(scriptStr);
                log.info("========== windows服务器 =========");
            } else if (Platform.isLinux()) {
                log.info("=========== linux服务器 ===========");
                process = Runtime.getRuntime().exec(linuxScript);
            }
            process.getOutputStream().close();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(),"gbk"));
            StringBuffer stringBuffer = new StringBuffer();
            String line = "";
            while (null != (line = reader.readLine())) {
                stringBuffer.append(line + "\n");
            }
            return stringBuffer.toString();
        } catch (IOException e) {
            log.error("========== 服务器执行命令异常 msg:{} ===========", e.getMessage());
        }
        return null;
    }

    public static String executeScript(String script, String[] linuxScript) {
        Process process = null;
        try {
            if (Platform.isWindows()) {
                process = Runtime.getRuntime().exec(script);
                log.info("========== windows服务器 =========");
            } else if (Platform.isLinux()) {
                log.info("=========== linux服务器 ===========");
                process = Runtime.getRuntime().exec(linuxScript);
            }
            process.getOutputStream().close();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuffer stringBuffer = new StringBuffer();
            String line = "";
            while (null != (line = reader.readLine())) {
                stringBuffer.append(line + "\n");
            }
            return stringBuffer.toString();
        } catch (IOException e) {
            log.error("========== 服务器执行命令异常 msg:{} ===========", e.getMessage());
        }
        return null;
    }

    /**
     * @Author xujian
     * @Description byte 转换成kb或者mb或者gb
     * @Date 10:47 2020/12/30
     * @Param [byteNumber]
     * @return java.lang.String
     **/
    public static String formatByte(long byteNumber) {
        double kbNumber = byteNumber / FORMAT;
        if (kbNumber < FORMAT) {
            return new DecimalFormat("#.##KB").format(kbNumber);
        }
        double mbNumber = kbNumber / FORMAT;
        if (mbNumber < FORMAT) {
            return new DecimalFormat("#.##MB").format(mbNumber);
        }
        double gbNumber = mbNumber / FORMAT;
        if (gbNumber < FORMAT) {
            return new DecimalFormat("#.##GB").format(gbNumber);
        }
        double tbNumber = gbNumber / FORMAT;
        return new DecimalFormat("#.##TB").format(tbNumber);
    }
}
